#include "precompile.h"

#include "Interaction.h"
#include "interaction/ISelection.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

Interaction::Interaction()
{
	m_activeRegion = 0;
	m_activeSelection = NULL;
}

Interaction::~Interaction()
{
}

void Interaction::ActiveView(const Points& pts)
{
	m_pts = pts;
}

uint32 Interaction::ActiveRegion() const
{
	return m_activeRegion;
}

void Interaction::ActiveRegion(uint32 value)
{
	m_activeRegion = value;
}

ISelection* Interaction::ActiveSelection() const
{
	if (!m_activeSelection)
		return NULL;
	return sys::InterfacePtr<ISelection>(m_activeSelection);
}

ISelection* Interaction::ActiveSelection(vclsid selection/*=CLSID_Selection*/)
{
	IObject* pSeletionObj = NULL;

	Selections::iterator itfind = m_selections.find(selection);
	if(itfind == m_selections.end())
	{
		sys::ObjectPtr objSelection;
		VRVERIFY(objSelection.CreateObject(selection));
		if (objSelection.IsNotNull())
			m_selections[selection] = objSelection;

		pSeletionObj = objSelection.P();
	}
	else
		pSeletionObj = itfind->second.P();;

	sys::InterfacePtr<ISelection> pIFSelection(pSeletionObj);
	if (m_activeSelection != pSeletionObj)
	{
		pIFSelection->DeSelectAll();
		m_activeSelection = pSeletionObj;
	}

	return pIFSelection;
}

Point Interaction::CalcRelativePoint(const Point& ptAbsolute)
{
	Point ptActiveRegion = m_pts[m_activeRegion];
	Point rpt = ptAbsolute;
	rpt.x -= ptActiveRegion.x;
	rpt.y -= ptActiveRegion.y;

	return rpt;
}